﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WXWeb.Admin
{
    public partial class EditUserInfo : BasePage
    {
        private Access access = new Access();
        MerchantsBO mbo = new MerchantsBO();
        private static int id;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                DataSet dt = mbo.GetList(" id <>6 ");
                foreach (DataRow dr in dt.Tables[0].Rows)
                {
                    ddlMerchants.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                }
                if (GetQeuryString("para") == "CurrentUser")//当前人的信息
                {
                    txtUID.Enabled = false;
                    InitData(CurrentUserInfo);
                }
                else if (GetQeuryString("para") == "add")
                {
                    this.txtUID.Attributes["validType"] = "isexite('EditUserInfo.aspx?para=IsExiteUser')";
                    this.txtCreateTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
                    this.txtUpdateTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
                }
                else if (GetQeuryString("para") == "update")
                {
                    txtUID.Enabled = false;
                    string id = GetQeuryString("id");
                    UserInfo user = access.GetUserByID(id);
                    InitData(user);
                }
                else if (GetQeuryString("para") == "IsExiteUser")
                {
                    txtUID.Enabled = false;
                    Response.Clear();
                    string uid = GetQeuryString("uid");
                    try
                    {
                        DataSet ds = access.SelectUserByUID(uid);
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            Response.Write("exite");
                        }
                        else
                        {
                            Response.Write("noexite");
                        }
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }
                    finally
                    {
                        Response.End();
                    }
                }
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            UserInfo user = GetEntity();
            if (GetQeuryString("option") == "update")
            {
                user.ID = id;
                access.UpdateUserInfo(user);
                Session["UserInfo"] = user;
                AlertAndClose("保存成功！");
            }
            else if (GetQeuryString("para") == "add")
            {
                if (access.GetUserByUID(user.UID) != null)
                {
                    AlertMessage("用户名已存在");
                }
                else
                {
                    access.AddUser(user);
                    AlertAndCloseAndReloadParendDataGrid("保存成功！", "grid");
                }
            }
            else if (GetQeuryString("para") == "update")
            {
                user.ID = Convert.ToInt32(GetQeuryString("id"));
                access.UpdateUserInfo(user);
                AlertAndCloseAndReloadParendDataGrid("保存成功！", "grid");
            }
        }

        #region 私有方法
        private UserInfo GetEntity()
        {
            UserInfo user = new UserInfo();
            user.UserName = this.txtUserName.Text;
            user.UID = this.txtUID.Text;
            user.PWD = this.txtPWD.Text;
            user.Sex = this.MAN.Checked ? "男" : "女";
            user.CreateTime = Convert.ToDateTime(this.txtCreateTime.Text);
            user.UpdateTime = Convert.ToDateTime(this.txtUpdateTime.Text);
            user.Skin = this.ddSkin.SelectedValue.ToString();
            user.MID = int.Parse(ddlMerchants.SelectedValue);
            return user;
        }
        private void InitData(UserInfo CurrentUserInfo)
        {
            this.txtUserName.Text = CurrentUserInfo.UserName;
            this.txtUID.Text = CurrentUserInfo.UID;
            this.txtPWD.Attributes["value"] = this.txtPWD2.Attributes["value"] = CurrentUserInfo.PWD;
            if (CurrentUserInfo.Sex == "男")
                this.MAN.Checked = true;
            else
                this.WOMAN.Checked = true;
            this.txtCreateTime.Text = CurrentUserInfo.CreateTime.ToString("yyyy-MM-dd");
            this.txtUpdateTime.Text = CurrentUserInfo.UpdateTime == null ? "" : CurrentUserInfo.UpdateTime.ToString("yyyy-MM-dd");
            this.ddSkin.SelectedValue = CurrentUserInfo.Skin;
            ddlMerchants.SelectedValue = CurrentUserInfo.MID.ToString();
            id = CurrentUserInfo.ID;
        }

        #endregion
    }
}